package indi.zhifa.study2025.oa.admin.facade.user.impl;

import indi.zhifa.engine.core.common.entity.web.ServiceException;
import indi.zhifa.engine.core.common.utils.DtoEntityUtil;
import indi.zhifa.engine.mpenhance.utils.DbDtoEntityUtil;
import indi.zhifa.study2025.oa.admin.facade.user.IUserFacade;
import indi.zhifa.study2025.oa.common.util.IPasswdUtil;
import indi.zhifa.study2025.oa.dao.mybatis.role.mapperOut.RoleSimpleMapperOut;
import indi.zhifa.study2025.oa.dao.po.GenRelUserRoleEntity;
import indi.zhifa.study2025.oa.dao.po.GenUserEntity;
import indi.zhifa.study2025.oa.model.enums.EUserStatus;
import indi.zhifa.study2025.oa.model.expose.role.vo.RoleVo;
import indi.zhifa.study2025.oa.model.expose.user.dto.CreateUserDto;
import indi.zhifa.study2025.oa.model.expose.user.vo.CreateUserVo;
import indi.zhifa.study2025.oa.model.expose.user.vo.UserInfo;
import indi.zhifa.study2025.oa.service.role.IRoleService;
import indi.zhifa.study2025.oa.service.user.IUserService;
import indi.zhifa.study2025.oa.service.user.dto.ChangeStatusDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Component
public class UserFacadeImpl implements IUserFacade {

    final IUserService mUserService;
    final IRoleService mRoleService;
    final IPasswdUtil mPasswdUtil;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CreateUserVo createUser(CreateUserDto pCreateUserDto) {
        // 检查用户是否重复
        if(mUserService.exists(pCreateUserDto.getUserName())){
            throw new ServiceException("已经有名为"+pCreateUserDto.getUserName()+"的用户");
        }
        // 加密密码
        String encodePasswd = mPasswdUtil.encodePasswd(pCreateUserDto.getPasswd());
        pCreateUserDto.setPasswd(encodePasswd);
        // 存储用户
        GenUserEntity userEntity = DbDtoEntityUtil.createFromDto(pCreateUserDto, GenUserEntity.class);
        userEntity.setCurVersion("1.0.0");
        userEntity.setStatus(EUserStatus.NORMAL);
        userEntity.setHasDep(false);
        userEntity.setHasPosition(false);
        mUserService.save(userEntity);
        // 处理角色绑定
        mUserService.setRoles(userEntity.getId(),pCreateUserDto.getRoleIds());
        // 处理部门绑定
        mUserService.setDeps(userEntity.getId(),pCreateUserDto.getDepIds());
        // 处理职位绑定
        mUserService.setPositions(userEntity.getId(),pCreateUserDto.getPositionIds());
        // 更新当前职位和部门
        ChangeStatusDto changeStatusDto = DtoEntityUtil.trans(pCreateUserDto, ChangeStatusDto.class);
        mUserService.changeStatus(userEntity.getId(),changeStatusDto);

        CreateUserVo rtn = DtoEntityUtil.trans(userEntity, CreateUserVo.class);
        return rtn;
    }

    @Override
    public UserInfo info(Long pId) {

        GenUserEntity userEntity = mUserService.checkById(pId);
        UserInfo userInfo = DtoEntityUtil.trans(userEntity, UserInfo.class);
        List<RoleSimpleMapperOut> roleSimpleMapperOutList = mRoleService.listByUserId(pId);
        List<RoleVo> roleSimpleVoList = DtoEntityUtil.trans(roleSimpleMapperOutList, RoleVo.class);
        userInfo.setRoles(roleSimpleVoList);
        return userInfo;
    }

    @Override
    public List<GenRelUserRoleEntity>  relRole(Long pUserId, List<Long> pRoleIdList) {
       return mUserService.setRoles(pUserId,pRoleIdList);
    }


}
